﻿<!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
-->

<UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  x:Class="System.Windows.Controls.Samples.CalendarSample">
    <StackPanel>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            
            <TextBlock Grid.Row="0" Grid.Column="0" Text="Selected Date:" />
            <TextBox  Grid.Row="0" Grid.Column="1" x:Name="txtSelectedDate" IsReadOnly="true" />
            
            <TextBlock Grid.Row="1" Grid.Column="0" Text="Current Display:" />
            <TextBox  Grid.Row="1" Grid.Column="1" x:Name="txtDisplayDate" IsReadOnly="true" />
            
            <TextBlock Grid.Row="2" Grid.Column="0" Text="Display Range Start:" />
            <controls:DatePicker
              x:Name="dateStart"
              Grid.Row="2"
              Grid.Column="1"
              SelectedDateChanged="OnDisplayDateStartSelected"
              Margin="5" />
            
            <TextBlock Grid.Row="3" Grid.Column="0" Text="Display Range End:" />
            <controls:DatePicker
              x:Name="dateEnd"
              Grid.Row="3"
              Grid.Column="1"
              SelectedDateChanged="OnDisplayDateEndSelected"
              Margin="5" />
            
            <CheckBox 
              x:Name="chkPastDateSelection"
              Grid.Row="0"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              IsChecked="True"
              Checked="OnPastDatesChanged"
              Unchecked="OnPastDatesChanged"
              Content="Allow the past be selectable"
              Margin="5" />
            
            <RadioButton
              x:Name="radioNone"
              GroupName="Selection"
              Grid.Row="1"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              Checked="OnSelectionModeChanged"
              Content="No Selection Allowed"
              Margin="5" />
            <RadioButton
              GroupName="Selection"
              Grid.Row="2"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              Checked="OnSelectionModeChanged"
              IsChecked="True"
              Content="Single Date Selection"
              Margin="5" />
            <RadioButton
              x:Name="radioSingleRange"
              GroupName="Selection"
              Grid.Row="3"
              Grid.Column="2"
              Grid.ColumnSpan="2"  
              Checked="OnSelectionModeChanged"
              Content="Contiguous Date Selection"
              Margin="5" />
            <RadioButton
              x:Name="radioMultiRange" 
              GroupName="Selection"
              Grid.Row="4"
              Grid.Column="2"
              Grid.ColumnSpan="2"
              Checked="OnSelectionModeChanged"
              Content="Disjoint Date Selection"
              Margin="5" />
            
            <TextBlock
              Text="Calendar bound to above"
              Grid.Row="5"
              Grid.Column="0"
              Grid.ColumnSpan="2" />
            <controls:Calendar
              x:Name="sampleCalendar"
              Grid.Row="6"
              Grid.Column="0"
              Grid.ColumnSpan="2"
              SelectedDatesChanged="OnSelectedDateChanged"
              DisplayDateChanged="OnDisplayDateChanged"
              Margin="5" />
            
            <TextBlock
              Text="Calendar in &quot;Year&quot; mode"
              Grid.Row="5"
              Grid.Column="2"
              Grid.ColumnSpan="2" />
            <controls:Calendar
              Grid.Row="6"
              Grid.Column="2"
              Grid.ColumnSpan="2"
              DisplayMode="Year"
              Margin="5" />
        </Grid>
        <src:SourceViewer xmlns:src="clr-namespace:System.Windows.Controls.Samples;assembly=System.Windows.Controls.Samples.Common" xmlns:sys="clr-namespace:System;assembly=mscorlib">
  <src:SourceFile Path="CalendarSample.xaml">
    <src:SourceFile.Source>
      <sys:String>&lt;!--
// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.
--&gt;

&lt;UserControl
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
  x:Class="System.Windows.Controls.Samples.CalendarSample"&gt;
    &lt;StackPanel&gt;
        &lt;Grid&gt;
            &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="Auto" /&gt;
                &lt;ColumnDefinition Width="*" /&gt;
                &lt;ColumnDefinition Width="Auto" /&gt;
                &lt;ColumnDefinition Width="Auto" /&gt;
            &lt;/Grid.ColumnDefinitions&gt;
            &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
                &lt;RowDefinition Height="Auto" /&gt;
            &lt;/Grid.RowDefinitions&gt;
            
            &lt;TextBlock Grid.Row="0" Grid.Column="0" Text="Selected Date:" /&gt;
            &lt;TextBox  Grid.Row="0" Grid.Column="1" x:Name="txtSelectedDate" IsReadOnly="true" /&gt;
            
            &lt;TextBlock Grid.Row="1" Grid.Column="0" Text="Current Display:" /&gt;
            &lt;TextBox  Grid.Row="1" Grid.Column="1" x:Name="txtDisplayDate" IsReadOnly="true" /&gt;
            
            &lt;TextBlock Grid.Row="2" Grid.Column="0" Text="Display Range Start:" /&gt;
            &lt;controls:DatePicker
              x:Name="dateStart"
              Grid.Row="2"
              Grid.Column="1"
              SelectedDateChanged="OnDisplayDateStartSelected"
              Margin="5" /&gt;
            
            &lt;TextBlock Grid.Row="3" Grid.Column="0" Text="Display Range End:" /&gt;
            &lt;controls:DatePicker
              x:Name="dateEnd"
              Grid.Row="3"
              Grid.Column="1"
              SelectedDateChanged="OnDisplayDateEndSelected"
              Margin="5" /&gt;
            
            &lt;CheckBox 
              x:Name="chkPastDateSelection"
              Grid.Row="0"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              IsChecked="True"
              Checked="OnPastDatesChanged"
              Unchecked="OnPastDatesChanged"
              Content="Allow the past be selectable"
              Margin="5" /&gt;
            
            &lt;RadioButton
              x:Name="radioNone"
              GroupName="Selection"
              Grid.Row="1"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              Checked="OnSelectionModeChanged"
              Content="No Selection Allowed"
              Margin="5" /&gt;
            &lt;RadioButton
              GroupName="Selection"
              Grid.Row="2"
              Grid.Column="2"
              Grid.ColumnSpan="2" 
              Checked="OnSelectionModeChanged"
              IsChecked="True"
              Content="Single Date Selection"
              Margin="5" /&gt;
            &lt;RadioButton
              x:Name="radioSingleRange"
              GroupName="Selection"
              Grid.Row="3"
              Grid.Column="2"
              Grid.ColumnSpan="2"  
              Checked="OnSelectionModeChanged"
              Content="Contiguous Date Selection"
              Margin="5" /&gt;
            &lt;RadioButton
              x:Name="radioMultiRange" 
              GroupName="Selection"
              Grid.Row="4"
              Grid.Column="2"
              Grid.ColumnSpan="2"
              Checked="OnSelectionModeChanged"
              Content="Disjoint Date Selection"
              Margin="5" /&gt;
            
            &lt;TextBlock
              Text="Calendar bound to above"
              Grid.Row="5"
              Grid.Column="0"
              Grid.ColumnSpan="2" /&gt;
            &lt;controls:Calendar
              x:Name="sampleCalendar"
              Grid.Row="6"
              Grid.Column="0"
              Grid.ColumnSpan="2"
              SelectedDatesChanged="OnSelectedDateChanged"
              DisplayDateChanged="OnDisplayDateChanged"
              Margin="5" /&gt;
            
            &lt;TextBlock
              Text="Calendar in &amp;quot;Year&amp;quot; mode"
              Grid.Row="5"
              Grid.Column="2"
              Grid.ColumnSpan="2" /&gt;
            &lt;controls:Calendar
              Grid.Row="6"
              Grid.Column="2"
              Grid.ColumnSpan="2"
              DisplayMode="Year"
              Margin="5" /&gt;
        &lt;/Grid&gt;
    &lt;/StackPanel&gt;
&lt;/UserControl&gt;</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="CalendarSample.xaml.cs">
    <src:SourceFile.Source>
      <sys:String>// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Controls;

namespace System.Windows.Controls.Samples
{
    /// &lt;summary&gt;
    /// Sample page demonstrating the Calendar.
    /// &lt;/summary&gt;
    [Sample("Calendar", DifficultyLevel.Basic)]
    [Category("Calendar")]
    public partial class CalendarSample : UserControl
    {
        /// &lt;summary&gt;
        /// Initializes a new instance of the CalendarSample class.
        /// &lt;/summary&gt;
        public CalendarSample()
        {
            InitializeComponent();

            // Setting the DatePickers
            txtDisplayDate.Text = sampleCalendar.DisplayDate.ToShortDateString();
            txtSelectedDate.Text = sampleCalendar.SelectedDate.ToString();
        }

        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Simplifies sample.")]
        private void OnPastDatesChanged(object sender, RoutedEventArgs e)
        {
            if (sampleCalendar == null)
            {
                return;
            }

            if ((bool)chkPastDateSelection.IsChecked)
            {
                try
                {
                    sampleCalendar.BlackoutDates.AddDatesInPast();
                }
                catch
                {
                    chkPastDateSelection.IsChecked = true;
                }
            }
            else
            {
                sampleCalendar.BlackoutDates.Clear();
            }
        }
        
        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Simplifies sample.")]
        private void OnDisplayDateStartSelected(object sender, SelectionChangedEventArgs e)
        {
            if (sampleCalendar == null)
            {
                return;
            }

            try
            {
                sampleCalendar.DisplayDateStart = e.AddedItems[0] as DateTime?;
            }
            catch
            {
                sampleCalendar.DisplayDateStart = null;
                dateStart.Text = "";
            }
        }

        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "Simplifies sample.")]
        private void OnDisplayDateEndSelected(object sender, SelectionChangedEventArgs e)
        {
            if (sampleCalendar == null)
            {
                return;
            }

            try
            {
                sampleCalendar.DisplayDateEnd = e.AddedItems[0] as DateTime?;
            }
            catch
            {
                sampleCalendar.DisplayDateEnd = null;
                dateEnd.Text = "";
            }
        }

        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        private void OnDisplayDateChanged(object sender, CalendarDateChangedEventArgs e)
        {
            if (sampleCalendar != null)
            {
                txtDisplayDate.Text = sampleCalendar.DisplayDate.ToShortDateString();
            }
        }

        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        private void OnSelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (sampleCalendar != null &amp;&amp; sampleCalendar.SelectedDate != null)
            {
                txtSelectedDate.Text = sampleCalendar.SelectedDate.ToString();
            }
        }

        /// &lt;summary&gt;
        /// Inherited code: Requires comment.
        /// &lt;/summary&gt;
        /// &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
        /// &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
        [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Event handler attached in XAML.")]
        private void OnSelectionModeChanged(object sender, RoutedEventArgs e)
        {
            if (sampleCalendar != null)
            {
                sampleCalendar.SelectionMode =
                    (sender == radioNone) ? CalendarSelectionMode.None :
                    (sender == radioSingleRange) ? CalendarSelectionMode.SingleRange :
                    (sender == radioMultiRange) ? CalendarSelectionMode.MultipleRange :
                    CalendarSelectionMode.SingleDate;
            }
        }
    }
}</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
  <src:SourceFile Path="CalendarSample.xaml.vb">
    <src:SourceFile.Source>
      <sys:String>' (c) Copyright Microsoft Corporation.
' This source is subject to the Microsoft Public License (Ms-PL).
' Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
' All other rights reserved.

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Diagnostics.CodeAnalysis
Imports System.Windows
Imports System.Windows.Controls

''' &lt;summary&gt;
''' Sample page demonstrating the Calendar.
''' &lt;/summary&gt;
&lt;Sample("Calendar", DifficultyLevel.Basic), Category("Calendar")&gt; _
Partial Public Class CalendarSample
    Inherits UserControl
    ''' &lt;summary&gt;
    ''' Initializes a new instance of the CalendarSample class.
    ''' &lt;/summary&gt;
    &lt;System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors", Justification:="InitializeComponent Required by Visual Studio.")&gt; _
    Public Sub New()
        InitializeComponent()

        ' Setting the DatePickers
        txtDisplayDate.Text = sampleCalendar.DisplayDate.ToShortDateString()
        txtSelectedDate.Text = sampleCalendar.SelectedDate.ToString()
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML."), SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification:="Simplifies sample.")&gt; _
    Private Sub OnPastDatesChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If sampleCalendar Is Nothing Then
            Return
        End If

        If CBool(chkPastDateSelection.IsChecked) Then
            Try
                sampleCalendar.BlackoutDates.AddDatesInPast()
            Catch
                chkPastDateSelection.IsChecked = True
            End Try
        Else
            sampleCalendar.BlackoutDates.Clear()
        End If
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML."), SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification:="Simplifies sample.")&gt; _
    Private Sub OnDisplayDateStartSelected(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        If sampleCalendar Is Nothing Then
            Return
        End If

        Try
            sampleCalendar.DisplayDateStart = CType(e.AddedItems(0), Nullable(Of DateTime))
        Catch
            sampleCalendar.DisplayDateStart = Nothing
            dateStart.Text = ""
        End Try
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML."), SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification:="Simplifies sample.")&gt; _
    Private Sub OnDisplayDateEndSelected(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        If sampleCalendar Is Nothing Then
            Return
        End If

        Try
            sampleCalendar.DisplayDateEnd = CType(e.AddedItems(0), Nullable(Of DateTime))
        Catch
            sampleCalendar.DisplayDateEnd = Nothing
            dateEnd.Text = ""
        End Try
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML.")&gt; _
    Private Sub OnDisplayDateChanged(ByVal sender As Object, ByVal e As CalendarDateChangedEventArgs)
        If sampleCalendar IsNot Nothing Then
            txtDisplayDate.Text = sampleCalendar.DisplayDate.ToShortDateString()
        End If
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML.")&gt; _
    Private Sub OnSelectedDateChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        If sampleCalendar IsNot Nothing AndAlso sampleCalendar.SelectedDate IsNot Nothing Then
            txtSelectedDate.Text = sampleCalendar.SelectedDate.ToString()
        End If
    End Sub

    ''' &lt;summary&gt;
    ''' Inherited code: Requires comment.
    ''' &lt;/summary&gt;
    ''' &lt;param name="sender"&gt;Inherited code: Requires comment 1.&lt;/param&gt;
    ''' &lt;param name="e"&gt;Inherited code: Requires comment 2.&lt;/param&gt;
    &lt;SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification:="Event handler attached in XAML.")&gt; _
    Private Sub OnSelectionModeChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If sampleCalendar IsNot Nothing Then
            sampleCalendar.SelectionMode = If((sender Is radioNone), CalendarSelectionMode.None, _
                                              If((sender Is radioSingleRange), CalendarSelectionMode.SingleRange, _
                                                 If((sender Is radioMultiRange), CalendarSelectionMode.MultipleRange, _
                                                    CalendarSelectionMode.SingleDate)))
        End If
    End Sub
End Class</sys:String>
    </src:SourceFile.Source>
  </src:SourceFile>
</src:SourceViewer>
    </StackPanel>
</UserControl>